package com.binvshe.binvshe.mine;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.Account;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.account.ui.LoginActivity;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.literature.LiteratureManagementActivity;
import com.binvshe.binvshe.message.MessageEvent;
import com.binvshe.binvshe.mine.corporation.MyClubActivity;
import com.binvshe.binvshe.mine.follow.MyFollowActivity;
import com.binvshe.binvshe.mine.ticket.TicketActivity;
import com.binvshe.binvshe.mine.usercenter.UserCenterActivity;
import com.binvshe.binvshe.organization.JoinOrgActivity;
import com.bumptech.glide.Glide;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.frameworkbase.imageloader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {


    @Bind(R.id.mImgSetting)
    ImageView mImgSetting;
    @Bind(R.id.mImgHead)
    CircleImageView mImgHead;
    @Bind(R.id.mImgGender)
    ImageView mImgGender;
    @Bind(R.id.mTvName)
    TextView mTvName;
    @Bind(R.id.mBtnMySpace)
    Button mBtnMySpace;
    @Bind(R.id.mLytCorporation)
    LinearLayout mLytCorporation;
    @Bind(R.id.mLytFollow)
    LinearLayout mLytFollow;
    @Bind(R.id.mLytFans)
    LinearLayout mLytFans;
    @Bind(R.id.mLytCollection)
    LinearLayout mLytCollection;
    @Bind(R.id.mRltyHead)
    RelativeLayout mRltyHead;
    @Bind(R.id.mLytTickets)
    LinearLayout mLytTickets;
    @Bind(R.id.mTvCreatCorp)
    TextView mTvCreatCorp;
    @Bind(R.id.ivHeadBack)
    ImageView ivHeadBack;

    private boolean isInClub;
    private Account mAccount;

    public MineFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_mine, null);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (AccountManager.getCurrentAccount() != null) {
            setAccount(AccountManager.getCurrentAccount().getUserId());
            setUserInfo();
        } else {
            setDefaultInfo();
        }
    }

    private void setAccount(String userId) {
        IHttpRequest request = new MyHttpRequest(getContext());
        String requestUrl = String.format(UrlCenter.USER, userId);
        request.get(requestUrl, null, new DataRequestListener<Account>(Account.class) {
            @Override
            public void onSuccess(Account account) {
                AccountManager.setCurrentAccount(account);
            }

            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getClubMessage(MessageEvent.EventClub eventClub) {
        setUserInfo();
    }

    private void setDefaultInfo() {
        mImgGender.setImageResource(R.mipmap.icon_boys);
        mImgHead.setImageResource(R.mipmap.portrait);
        mTvCreatCorp.setText("创建/加入社团");
        mTvName.setText("请登录~");
    }

    private void setUserInfo() {
        mAccount = AccountManager.getCurrentAccount();
        Log.d("kyluzoi", "MineFragment.setUserInfo");
        if (!TextUtils.isEmpty(mAccount.getNickName())) {
            mTvName.setText(mAccount.getNickName());
        } else {
            mTvName.setText("");
        }
        if (mAccount.getGender() == 0 || mAccount.getGender() == 1) {
            switch (mAccount.getGender()) {
                case 0:
                    mImgGender.setImageResource(R.mipmap.icon_girls);
                    break;
                case 1:
                    mImgGender.setImageResource(R.mipmap.icon_boys);
                    break;
            }
        } else {
            mImgGender.setImageResource(R.mipmap.icon_boys);
        }

        isInClub = mAccount.getInClub();
        if (!isInClub || AccountManager.getCurrentAccount().getClub() == null) {
            mTvCreatCorp.setText("创建/加入社团");
        } else {
            mTvCreatCorp.setText("我的社团");
        }

        if (!TextUtils.isEmpty(mAccount.getPortrait())) {
            ImageLoader.with(getContext())
                    .load(mAccount.getPortrait())
                    .into(mImgHead)
                    .start();
        }
        Glide.with(this)
                .load(mAccount.getPortrait())
                .crossFade(1000)
                .bitmapTransform(new BlurTransformation(getActivity(), 23, 4)) // “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
                .into(ivHeadBack);

    }

    @OnClick({R.id.mImgSetting,
            R.id.mImgHead,
            R.id.mTvName,
            R.id.mBtnMySpace,
            R.id.mLytCorporation,
            R.id.mLytFollow,
            R.id.mLytFans,
            R.id.mLytCollection,
            R.id.mLytTickets,
            R.id.lytOpusManagement,
            R.id.mLytCoin})
    public void onClick(View view) {

        if (view.getId() != R.id.mImgSetting && AccountManager.getCurrentAccount() == null) {
            LoginActivity.start(getActivity());
            return;
        }

        switch (view.getId()) {
            case R.id.mImgSetting:
                //设置
                SettingActivity.start(getActivity());
                break;
            case R.id.mImgHead:
                //头像
                MineInfoActivity.start(getActivity());
                break;
            case R.id.mBtnMySpace:
                //我的空间
                UserCenterActivity.start(getContext(), mAccount.getUserId());
                break;
            case R.id.mLytCorporation:
                //社团
                if (!isInClub || AccountManager.getCurrentAccount().getClub() == null) {
                    JoinOrgActivity.start(getContext());
                } else {
                    MyClubActivity.start(getActivity());
                }
                break;
            case R.id.mLytFollow:
                //我的关注
                MyFollowActivity.start(getActivity());
                break;
            case R.id.mLytFans:
                //我的粉丝
                FansActivity.start(getActivity());
                break;
            case R.id.mLytCollection:
                //我的收藏
                CollectionActivity.start(getContext());
                break;
            case R.id.mLytTickets:
                //我的票券
                TicketActivity.start(getContext());
                break;
            case R.id.lytOpusManagement:
                //文学管理
                LiteratureManagementActivity.start(getContext());
                break;

            case R.id.mLytCoin:
                //萌都币
                MyCoinActivity.start(getActivity());
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}